package com.cloudptt.api.product.func.media.device;

import android.bluetooth.BluetoothHeadset;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.KeyEvent;

import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.func.media.EventListener;
import com.cloudptt.api.product.log.Log;

public class PNC370 implements IDeviceKey {
	public final static String THIS_FILE = "PNC370";
	private static final String BRAND = "qcom";
	private static final String DEVICE = "msm8909";
	private static final String MODEL = "PNC 370";

	private IntentFilter mFilter;

//	/**Imesh & iNet 网络通知**/
//	public static final String ACTION_NETWORK_STATE_CHANGED = "action.inet.network.state.changed";
//	public static final String KEY_NETWORK_STATE = "network_state";
//
//	/**配置信息下发通知**/
//	public static final String ACTION_ACCOUNT_CONFIG_CHANGED = "action.igst.account.config.changed";
//	public static final String KEY_IGST_ACCOUNT_CONFIG_DATA = "igst_account_data";
//
//	/**IGST开关**/
//	public static final String ACTION_IGST_STATUS_SWITCH_REQUEST = "action.igst.status.switch.request";
//	public static final String ACTION_IGST_STATUS_SWITCH_RESULT = "action.igst.status.switch.result";
//	public static final String KEY_IGST_STATUS_SWITCH_DATA = "igst_status_switch_data";
//	public static final String ACTION_IGST_START = "com.hytera.intent.action.IGST_START";
//
//	/**IGST状态查询  开关状态**/
//	public static final String ACTION_IGST_STATUS_QUERY_REQUEST = "action.igst.status.request";
//	public static final String ACTION_IGST_STATUS_QUERY_RESULT = "action.igst.status.result";
//	public static final String KEY_IGST_STATUS_QUERY_DATA = "igst_status_query_data";

	/**指环PTT**/
//	 private final static String ACTION_PTT_DOWN = "com.hytera.ptt.down";
	 private final static String ACTION_PTT = "com.meigsmart.meigkeyaccessibility.onkeyevent";

	 /**腕表信息*下发*/
//	 private static final String ACTION_WATCH_DEVICE_BIND_REQUEST = "action.watch.device.bind.request";
//	 private static final String KEY_WATCH_DEVICE_MAC = "igst_watch_device_mac";
//	 private static final String KEY_WATCH_DEVICE_BIND_OPT = "igst_watch_device_bind_opt";
//
//	 private static final String ACTION_PTT_WIRED = "com.hytera.ptt.action";
//	 private static final String KEY_VALUE = "key_value";

	 /**腕表绑定信息查询**/
//	 private static final String ACTION_WATCH_DEVICE_CONNECTED_REQUEST = "action.watch.device.connected.request";

	public PNC370(){
		
	}
	
	public static boolean isThisDev(String brand,String device,String model){
		if(brand == null
				|| device == null
				|| model == null){
			return false;
		}
		else if(BRAND.equals(brand)
				&& DEVICE.equals(device)
				&& MODEL.equals(model)){
			return true;
		}
		return false;
	}

	@Override
	public IntentFilter getBstIntentFilter() {
		mFilter = new IntentFilter();
		/**蓝牙PTT**/
		mFilter.addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
		mFilter.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY+"."+Integer.toString(0x1001));
		mFilter.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY+"."+Integer.toString(0x1002));
		mFilter.addAction(ACTION_PTT);/**蓝牙指环PTT**/
//		mFilter.addAction(ACTION_PTT_UP);/**蓝牙指环PTT**/
//
//		mFilter.addAction(ACTION_NETWORK_STATE_CHANGED);/**网络通知 IMESH & INET**/
//		mFilter.addAction(ACTION_ACCOUNT_CONFIG_CHANGED);/**配置信息 **/
//		mFilter.addAction(ACTION_WATCH_DEVICE_BIND_REQUEST);/**腕表信息**/
//
//		mFilter.addAction(ACTION_WATCH_DEVICE_CONNECTED_REQUEST);/**腕表状态查询**/
//		mFilter.addAction(ACTION_IGST_STATUS_SWITCH_REQUEST);/**IGST 开关指令**/
//		mFilter.addAction(ACTION_IGST_STATUS_QUERY_REQUEST);/**IGST状态查询**/
//
//		mFilter.addAction(ACTION_PTT_WIRED);/**IGST 有线耳咪**/
		
		return mFilter;
	}
	
	@Override
	public void procBst(Intent intent, EventListener listener) {
		String action = intent.getAction();
		Log.d(THIS_FILE, " procBst action = " + action);
		if(action.equals(ACTION_PTT)){
			int actionKey = intent.getIntExtra("action", -1);
			int keycode = intent.getIntExtra("keycode", -1);
//这里可以对按键做你想要的处理
			if(KeyEvent.KEYCODE_F12 == keycode) {
				if (actionKey == KeyEvent.ACTION_DOWN) {
					if (listener != null) {
						listener.onPtt(Const.MSG_TALK_ON, false, false,true,false);
					}
				} else if (actionKey == KeyEvent.ACTION_UP) {
					if (listener != null) {
						listener.onPtt(Const.MSG_TALK_OFF, false, false,true,false);
					}
				}
			}
//			Set<String> info = intent.getCategories();
//			Iterator it1 = info.iterator();
//			String strInfo = (String) it1.next();
//			String ptt_down = BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY+ "." + Integer.toString(0x1001);
//			String ptt_up = BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY+ "." + Integer.toString(0x1002);
//			if(ptt_down.equals(strInfo)){
//				Log.d(THIS_FILE, " PTT_DOWN");
//				if (listener != null) {
//					listener.onPtt(Const.MSG_TALK_ON,false);
//				}
//			}else if(ptt_up.equals(strInfo)){
//				Log.d(THIS_FILE, " PTT_UP");
//				if (listener != null) {
//					listener.onPtt(Const.MSG_TALK_OFF,false);
//				}
//			}
		}
//		else if(action.equals(ACTION_NETWORK_STATE_CHANGED)){
//			boolean isconnect = intent.getBooleanExtra(KEY_NETWORK_STATE, false);
//			Log.d(THIS_FILE, " ACTION_NETWORK_STATE_CHANGED  isconnect =" + isconnect);
//			if(listener!= null){
//				listener.networkChange(isconnect);
//			}
//		}else if(action.equals(ACTION_ACCOUNT_CONFIG_CHANGED)){
//			Log.d(THIS_FILE, " ACTION_ACCOUNT_CONFIG_CHANGED ");
//			byte[] configdata= intent.getByteArrayExtra(KEY_IGST_ACCOUNT_CONFIG_DATA);
//
//			if(configdata !=null && configdata.length >0){
//			String name = parseUserName(configdata);
//			String password = parsePassword(configdata);
//			String serverip = parseServerIP(configdata);
//			int port = parseServerPort(configdata);
//			Log.d(THIS_FILE, " ACTION_ACCOUNT_CONFIG_CHANGED name= "+name+",password="+password+",serverip="+serverip+",port="+port);
//
//			LoginInfoStruct data = new LoginInfoStruct(name,password,serverip,port);
//			if(listener!= null){
//				listener.onReqLogin(data);
//			}
//			}else{
//
//			}
//		}else if(action.equals(ACTION_IGST_STATUS_SWITCH_REQUEST)){
//			//0:open,1:close
//			int isopen = intent.getIntExtra(KEY_IGST_STATUS_SWITCH_DATA, 1);
//			Log.i(THIS_FILE, "ACTION_IGST_STATUS_SWITCH_REQUEST isopen =" +isopen);
//			if(listener!= null){
//				listener.onCloseApplication(isopen);
//			}
//		}else if(action.equals(ACTION_IGST_STATUS_QUERY_REQUEST)){
//			if(listener!= null){
//				listener.onIgstStatusAck();
//			}
//		}else if(action.equals(ACTION_PTT_DOWN)){
//			Log.d(THIS_FILE, " ACTION_PTT_DOWN");
//			if (listener != null) {
//				listener.onPtt(Const.MSG_TALK_ON,false);
//			}
//		}else if(action.equals(ACTION_PTT_UP)){
//			Log.d(THIS_FILE, " ACTION_PTT_UP");
//			if (listener != null) {
//				listener.onPtt(Const.MSG_TALK_OFF,false);
//			}
//		}else if(action.equals(ACTION_WATCH_DEVICE_BIND_REQUEST)){
//			String watchMac = intent.getStringExtra(KEY_WATCH_DEVICE_MAC);
//			int bind = intent.getIntExtra(KEY_WATCH_DEVICE_BIND_OPT, -1);
//
//			Log.i(THIS_FILE, " ACTION_WATCH_DEVICE_BIND_REQUEST watchMac= "+watchMac+",bind="+bind);
//			if(listener!= null){
//				listener.onReqConnectWatch(watchMac,bind);
//			}
//		}else if(action.equals(ACTION_WATCH_DEVICE_CONNECTED_REQUEST)){
//			Log.i(THIS_FILE, " ACTION_WATCH_DEVICE_CONNECTED_REQUEST");
//			if(listener!= null){
//				listener.onReqConnectWatchStatus();
//			}
//		}
//		else if(action.equals(ACTION_PTT_WIRED)){
//			int keyValue = intent.getIntExtra(KEY_VALUE,0);
//			KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
//			int repeatCount = event.getRepeatCount();
//			Log.e(THIS_FILE, " ACTION_PTT_WIRED keyValue = " + keyValue + " repeatCount = " + repeatCount);
//			if(repeatCount != 0)
//			{
//				return;
//			}
//			if (listener != null) {
//				listener.onPtt(keyValue,false);
//			}
//		}
		
	}
	
	/** 解析登录信息包 **/
	public static final int MSG_HEAD_LENGTH = 2 * 4;
	public static final int MSG_NAME_LENGTH = 8 * 4;
	public static final int MSG_PSW_LENGTH = 8 * 4;
	public static final int MSG_IP_LENGTH = 4 * 4;
	public static final int MSG_PORT_LENGTH = 4;

	public String parseUserName(byte[] msg) {
		if(msg == null || msg.length == 0)return null;
		int offset = MSG_HEAD_LENGTH;
		byte[] data = new byte[MSG_NAME_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_NAME_LENGTH);
		int len = 0;
		for(int i= 0;i <MSG_NAME_LENGTH;i++){
			if(data[i] !=0){
				len =len+1;
			}
		}
		if(len!=0){
			byte[] resultdata = new byte[len];
			System.arraycopy(data, 0, resultdata, 0, len);
			return new String(resultdata);
		}
		return "";
	}
	
	public String parsePassword(byte[] msg) {
		if(msg == null || msg.length == 0)return null;
		int offset = MSG_HEAD_LENGTH + MSG_NAME_LENGTH;
		byte[] data = new byte[MSG_PSW_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_PSW_LENGTH);
		
		int len = 0;
		for(int i= 0;i <MSG_PSW_LENGTH;i++){
			if(data[i] !=0){
				len =len+1;
			}
		}
		if(len!=0){
			byte[] resultdata = new byte[len];
			System.arraycopy(data, 0, resultdata, 0, len);
			return new String(resultdata);
		}
		return "";
	}
	
	public String parseServerIP(byte[] msg) {
		if(msg == null || msg.length == 0)return null;
		int offset = MSG_HEAD_LENGTH + MSG_NAME_LENGTH + MSG_PSW_LENGTH;
		byte[] data = new byte[MSG_IP_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_IP_LENGTH);
		
		int len = 0;
		for(int i= 0;i <MSG_IP_LENGTH;i++){
			if(data[i] !=0){
				len =len+1;
			}
		}
		if(len!=0){
			byte[] resultdata = new byte[len];
			System.arraycopy(data, 0, resultdata, 0, len);
			return new String(resultdata);
		}
		return "";
	}
	
	public int parseServerPort(byte[] msg) {
		if(msg == null || msg.length == 0)return 0;
		int offset = MSG_HEAD_LENGTH + MSG_NAME_LENGTH + MSG_PSW_LENGTH + MSG_IP_LENGTH;
		byte[] data = new byte[MSG_PORT_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_PORT_LENGTH);
		int port = byteArrayToInt(data);
		return port;
	}
	
	public int byteArrayToInt(byte[] b) {
		byte[] a = new byte[4];
		int i = a.length - 1;
		int j = b.length - 1;
		for (; i >= 0; i--, j--) {
			if (j >= 0) {
				a[i] = b[j];
			} else {
				a[i] = 0;
			}
		}

		// litter ending
		// int v0 = (a[0] & 0xFF) << 24;
		// int v1 = (a[1] & 0xFF) << 16;
		// int v2 = (a[2] & 0xFF) << 8;
		// int v3 = (a[3] & 0xFF);

		// big ending
		int v0 = (a[0] & 0xFF);
		int v1 = (a[1] & 0xFF) << 8;
		int v2 = (a[2] & 0xFF) << 16;
		int v3 = (a[3] & 0xFF) << 24;
		return v0 + v1 + v2 + v3;
	}

}
